package org.example.my.Utility;

import org.springframework.util.DigestUtils;

import java.util.UUID;

public class SecurityUtil {


    //加密
    public static String generateSalt(String  password) {
        // 生成盐
        String salt = UUID.randomUUID().toString().replace("-", "");
        // 密码加密
        String md5 = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        // 返回
        return md5+salt;

    }

    //解密
    public static boolean checkPassword(String password, String databasePassword) {
        // 获取盐
        String salt = databasePassword.substring(32);
        String md5 = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        return md5.equals(databasePassword.substring(0, 32));
    }


}
